package com.xiegang.servicedriveruser.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiegang.internalcommon.constant.CommonStatusEnum;
import com.xiegang.internalcommon.constant.DriverCarContants;
import com.xiegang.internalcommon.dto.*;
import com.xiegang.internalcommon.response.OrderDriverResponse;
import com.xiegang.servicedriveruser.mapper.CarMapper;
import com.xiegang.servicedriveruser.mapper.DriverCarBindingRelationshipMapper;
import com.xiegang.servicedriveruser.mapper.DriverUserMapper;
import com.xiegang.servicedriveruser.mapper.DriverUserWorkStatusMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author xieGang
 * @description
 * @date 2023/3/28 20:38
 **/
@Service
public class DriverUserService {

    @Resource
    private DriverUserMapper driverUserMapper;

    @Autowired
    private DriverUserWorkStatusMapper driverUserWorkStatusMapper;

    @Autowired
    private DriverCarBindingRelationshipMapper driverCarBindingRelationshipMapper;

    @Autowired
    private CarMapper carMapper;

    public ResponseResult testGetDriverUser(){

        DriverUser driverUser = driverUserMapper.selectById(1);
        return ResponseResult.success(driverUser);
    }

    public ResponseResult addUser(DriverUser driverUser){
        LocalDateTime now = LocalDateTime.now();
        driverUser.setGmtCreate(now);
        driverUser.setGmtModified(now);
        driverUserMapper.insert(driverUser);
        //初始化 司机工作状态
        DriverUserWorkStatus driverUserWorkStatus = new DriverUserWorkStatus();
        driverUserWorkStatus.setDriverId(driverUser.getId());
        driverUserWorkStatus.setWorkStatus(DriverCarContants.DRIVER_WORK_STATUS_STOP);
        driverUserWorkStatus.setGmtCreate(now);
        driverUserWorkStatus.setGmtModified(now);
        driverUserWorkStatusMapper.insert(driverUserWorkStatus);
        return ResponseResult.success();
    }

    public ResponseResult updateUser(DriverUser driverUser){

        driverUserMapper.updateById(driverUser);
        return ResponseResult.success();
    }

    public ResponseResult<DriverUser> getUser(String driverPhone){
        Map<String, Object> map = new HashMap<>();
        map.put("driver_phone",driverPhone);
        map.put("state", DriverCarContants.DRIVER_STATE_VALID);
        List<DriverUser> driverUsers = driverUserMapper.selectByMap(map);
        if(driverUsers.isEmpty()){
            return ResponseResult.fail(CommonStatusEnum.DRIVER_NOT_EXISTS.getCode(),CommonStatusEnum.DRIVER_NOT_EXISTS.getMessage());

        }
        return ResponseResult.success(driverUsers.get(0));
    }

    public ResponseResult<OrderDriverResponse> getAvailableDriver(Long carId) {

        QueryWrapper<DriverCarBindingRelationship> DriverCarBindingRelationshipQueryWrapper = new QueryWrapper<>();
        DriverCarBindingRelationshipQueryWrapper.eq("car_id",carId);
        DriverCarBindingRelationshipQueryWrapper.eq("bind_state",DriverCarContants.DRIVER_CAR_BIND);
        DriverCarBindingRelationship driverCarBindingRelationship = driverCarBindingRelationshipMapper.selectOne(DriverCarBindingRelationshipQueryWrapper);
        Long driverId = driverCarBindingRelationship.getDriverId();

        QueryWrapper<DriverUserWorkStatus> driverUserWorkStatusQueryWrapper = new QueryWrapper<>();
        driverUserWorkStatusQueryWrapper.eq("driver_id",driverId);
        driverUserWorkStatusQueryWrapper.eq("work_status",DriverCarContants.DRIVER_WORK_STATUS_STAT);

        DriverUserWorkStatus driverUserWorkStatus = driverUserWorkStatusMapper.selectOne(driverUserWorkStatusQueryWrapper);
        if(Objects.isNull(driverUserWorkStatus)){
            return ResponseResult.fail(CommonStatusEnum.AVAILABLE_DRIVER_TMPTY.getCode(),CommonStatusEnum.AVAILABLE_DRIVER_TMPTY.getMessage());
        }

        QueryWrapper<DriverUser> driverUserQueryWrapper = new QueryWrapper<>();
        driverUserQueryWrapper.eq("id",driverId);
        DriverUser driverUser = driverUserMapper.selectOne(driverUserQueryWrapper);

        QueryWrapper<Car> carQueryWrapper = new QueryWrapper<>();
        carQueryWrapper.eq("id",carId);
        Car car = carMapper.selectOne(carQueryWrapper);

        OrderDriverResponse orderDriverResponse = new OrderDriverResponse();
        orderDriverResponse.setCarId(carId);
        orderDriverResponse.setDriverId(driverId);
        orderDriverResponse.setDriverPhone(driverUser.getDriverPhone());
        orderDriverResponse.setLicenseId(driverUser.getLicenseId());
        orderDriverResponse.setVehicleNo(car.getVehicleNo());

        return ResponseResult.success(orderDriverResponse);
    }
}
